import apiClient from './axios'

export default {
  login(credentials) {
    const params = new URLSearchParams()
    params.append('username', credentials.username)
    params.append('password', credentials.password)

    return apiClient.post('/auth/login', params, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
    })
  },
  register(userData) {
    const user = {
      username: userData.username,
      passwordHash: userData.password,
      email: userData.email,
      phone: userData.phone,
      realName: userData.realName,
    }

    return apiClient.post('/auth/register', user)
  },
  // 验证 token 有效性
  validateToken() {
    return apiClient.post(
      '/auth/validate-token',
      {},
      {
        headers: {
          Authorization: `Bearer ${localStorage.getItem('token')}`,
        },
      },
    )
  },
  logout() {
    return apiClient.post('/auth/logout')
  },
}
